﻿using AutoMapper;
using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Write.API.Applications.Commands.Traceabilitys.DeepProcessings;
using MediatR;

namespace B.S.RbacData.Write.API.Applications.Commandhandlters.Traceabilitys.DeepProcessings
{

    /// <summary>
    /// 添加深加工企业信息命令处理
    /// </summary>
    public class AddDeepProcessingCorporateCommandHandlter : IRequestHandler<AddDeepProcessingCorporateCommand, ApiResult<int>>
    {
        /// <summary>
        /// 深加工仓储
        /// </summary>
        private readonly IBaseRepository<DeepProcessingCorporateModel> deepProcessingRep;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="deepProcessingRep">深加工仓储</param>
        /// <param name="mapper">映射</param>
        public AddDeepProcessingCorporateCommandHandlter(IBaseRepository<DeepProcessingCorporateModel> deepProcessingRep, IMapper mapper)
        {
            this.deepProcessingRep = deepProcessingRep;
            this.mapper = mapper;
        }


        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiResult<int>> Handle(AddDeepProcessingCorporateCommand request, CancellationToken cancellationToken)
        {

            //新增深加工信息
            var deepProcessing = mapper.Map<DeepProcessingCorporateModel>(request);
            deepProcessing.CreaTime = DateTime.Now;

            await deepProcessingRep.AddAsync(deepProcessing);


            return new ApiResult<int>
            {
                code = ResultEnumCode.Ok,
                data = 1,
                message = "新增深加工信息成功"

            };
        }
    }


}
